package common;


import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;


/**
 * @ClassName AccessUserNameFilter
 * @Description TODO
 * @Author xiguan
 * @Date 2021/12/30 12:33
 * @Version 1.0.0
 **/
@Component
public class AccessUserNameFilter extends ZuulFilter{


    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 判断请求的接口是否需要验证token
     * 在这里，我们设定 /api/user/login，不需要验证token
     * 若不需要验证，返回false，
     * 需要验证，返回true
     *
     */
    @Override
    public boolean shouldFilter() {
        //1. 获取Zuul提供的请求上下文对象
        RequestContext requestContext = RequestContext.getCurrentContext();
        // 获取request对象
        HttpServletRequest request = requestContext.getRequest();
        if("/api/user/login".equals(request.getRequestURI())){
            return false;
        }else if(request.getMethod().equals("OPTIONS")){
            return false;
        }else {
            return true;
        }
    }


    @Override
    public Object run() throws ZuulException {

        RequestContext requestContext = RequestContext.getCurrentContext();
        //从请求中获取request域
        HttpServletRequest request = requestContext.getRequest();
        //从request域里面获得Authorization字段下的值（我就是放Bearer + token信息）
        String header = request.getHeader("Authorization");
        System.out.println(header);
        if(null != header && !"".equals(header)){
            //把信息头转发到下一个微服务
            //requestContext.addZuulRequestHeader("Authorization",header);
            System.out.println("zhuanfa");
            requestContext.addZuulRequestHeader("header",header);
        }
        return null;
    }

//    /**
//     * 若shouldFilter返回true，则运行run方法，来验证token
//     * */
//    @Override
//    public Object run() throws ZuulException {
//
//        RequestContext requestContext = RequestContext.getCurrentContext();
//        HttpServletRequest request = requestContext.getRequest();
//
//        // 获取token值
//        String token = request.getHeader("authorization");
//        System.out.println(token);
//        // 判断是否为空
//        if(token == null || "".equals(token.trim())){
//            //没有token，验证失败
//            requestContext.setSendZuulResponse(false);
//            requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
//        }else {
//            try {
//                //验证token是否合格
//                boolean tokenVerify = TokenUtil.verify(token);
//                //如果token合格，根据请求，寻找路由
//                if(tokenVerify){
//                    requestContext.setSendZuulResponse(true);
//                }else {
//                    requestContext.setSendZuulResponse(false);
//                    requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
//                }
//            }catch (Exception e){
//                requestContext.setSendZuulResponse(false);
//                requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
//            }
//        }
//        return null;
//    }
}
